package com.qire.manhua.controller;

import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.AdvertManage;
import com.qire.common.constant.Navigation;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.databinding.FragmentComicDetailDirectoryBinding;
import com.qire.manhua.databinding.LayoutComicDetailDirectoryItemBinding;
import com.qire.manhua.dialog.UnlockChapterOnComicDetailDialog;
import com.qire.manhua.fragment.ComicDetailDirectoryFragment;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.entity.ComicBookEntity.DirectoryInfo;
import com.qire.manhua.model.viewModel.impl.ComicDetailDirectoryViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDetailDirectoryViewModel.ComicDetailDirectoryItemViewModel;
import com.qire.manhua.model.viewModel.impl.UnlockChapterDialogViewModel;

import androidx.annotation.NonNull;

/**
 * 事件控制器
 * 漫画书详情页->目录选项卡
 */
public class ComicDetailDirectoryController extends EventController<ComicDetailDirectoryFragment> implements AutoVariable<LayoutComicDetailDirectoryItemBinding>, ComicDetailUnlockController {

    private ComicDetailDirectoryViewModel viewModel;

    public ComicDetailDirectoryController(@NonNull ComicDetailDirectoryFragment lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        FragmentComicDetailDirectoryBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setComicDetailDirectoryController(this);
        }
    }

    @Override
    public void bindVariable(LayoutComicDetailDirectoryItemBinding viewBind) {
        viewBind.setComicDirectoryController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicDetailDirectoryViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void sortBtnOnClick(View view) {
        viewModel.toggleSort();
    }

    public void directoryItemOnClick(View view) {
        ComicDetailDirectoryItemViewModel selectorItemViewModel = (ComicDetailDirectoryItemViewModel) view.getTag();
        DirectoryInfo directoryInfo = selectorItemViewModel.directoryInfo;
        if(selectorItemViewModel == null) {
            return;
        }
        if(directoryInfo == null) {
            return;
        }

        ComicBookEntity comicBookEntity = viewModel.comicBookEntity();
        comicBookEntity.chaptersReading(directoryInfo.chapterId);

        viewModel.refreshReadChapterInfo();

        // 拦截未解锁的章节
        if(directoryInfo.haveLock()) {
            UnlockChapterDialogViewModel unlockDialogViewModel = new UnlockChapterDialogViewModel();
            unlockDialogViewModel.loadComicData(comicBookEntity, directoryInfo);

            boolean noInsufficientBalance = !unlockDialogViewModel.isInsufficientBalanceByPanel();
            boolean isAutoUnlock = UserEntity.self.isAutoPay();
            if(!(noInsufficientBalance && isAutoUnlock)) {
                UnlockChapterOnComicDetailDialog.build(owner.getActivity(), unlockDialogViewModel, this).show();
                return;
            }
        }

        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                .withData("comicBookEntity", comicBookEntity)
                .navigation();
    }

    @Override
    public void unlockByAdBtnOnClick(View view) {
        UnlockChapterDialogViewModel unlockChapterViewModel = ObjectUtil.as(view.getTag(), UnlockChapterDialogViewModel.class);
        if(ObjectUtil.nonNull(unlockChapterViewModel)) {
            final int chapterId = unlockChapterViewModel.getChapterId();
            viewModel.showLoadDialog();
            AdvertManage.AdvertSpace.ComicUnlock.build()
                    .setSize(EquipmentInfoUtil.getScreenWidth(), 0)
//                    .setClosedListener(() -> ToastHelper.showToast("您取消了视频播放，放弃了解锁奖励！"))
                    .setClosedListener(() -> viewModel.unlockChapterByAd(chapterId))
                    .setErrorListener(() -> {
                        viewModel.dismissLoadDialog();
                        ToastHelper.showToast("运行错误，请联系客服或稍后再试！");
                    })
                    .setStartListener(viewModel::dismissLoadDialog)
                    .playVideo(owner.getActivity());
        }
    }

    @Override
    public void unlockByBuyBtnOnClick(View view) {
        UnlockChapterDialogViewModel unlockChapterViewModel = ObjectUtil.as(view.getTag(), UnlockChapterDialogViewModel.class);
        if(ObjectUtil.nonNull(unlockChapterViewModel)) {
            int     chapterId       = unlockChapterViewModel.getChapterId();
            String  chapterPrice    = unlockChapterViewModel.getBuyPriceStr();
            String  paymentType     = unlockChapterViewModel.isBeanUnlockPanel() ? "bean" : "coin";
            viewModel.unlockChapterByBuy(chapterId, chapterPrice, paymentType);
        }
    }
}
